package com.whoimi.admin.rest;

import com.whoimi.admin.entity.AuthenticationToken;
import com.whoimi.admin.service.AuthenticationService;
import com.whoimi.common.ResultInfo;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/auth")
public class AuthenticationRest {
    private static final Logger log = LoggerFactory.getLogger(AuthenticationRest.class);
    @Resource
    private AuthenticationService authenticationService;

    public record CaptchaInfo(String captchaKey, String captchaBase64) {

    }

    @GetMapping("/captcha")
    public ResultInfo<CaptchaInfo> getCaptcha() {
        CaptchaInfo captcha = authenticationService.getCaptcha();
        return ResultInfo.ok(captcha);
    }

    @PostMapping("/login")
    public ResultInfo<AuthenticationToken> login(@RequestParam String username, @RequestParam String password) {
        AuthenticationToken authenticationToken = new AuthenticationToken();
        log.info("username: {}, password: {}", username, password);
        authenticationToken.setAccessToken("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsImRlcHRJZCI6MSwiZGF0YVNjb3BlIjoxLCJ0b2tlblR5cGUiOmZhbHNlLCJleHAiOjE3NTM0MzYyOTAsInVzZXJJZCI6MiwiaWF0IjoxNzUzNDI5MDkwLCJhdXRob3JpdGllcyI6WyJST0xFX0FETUlOIl0sImp0aSI6IjUzNWY3ODFhMjViMjRhMzNiNjYxOGM4YzRhZmEwZGVlIn0.26srCvx0w-oST-IKzWlne4_D7hs-SSgkcCbf9mpzZR8");
        authenticationToken.setRefreshToken("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsImRlcHRJZCI6MSwiZGF0YVNjb3BlIjoxLCJ0b2tlblR5cGUiOnRydWUsImV4cCI6MTc1NDAzMzg5MCwidXNlcklkIjoyLCJpYXQiOjE3NTM0MjkwOTAsImF1dGhvcml0aWVzIjpbIlJPTEVfQURNSU4iXSwianRpIjoiNWY2MmU5M2NiN2Y1NGYwZTgzNTY0OTMwOTliYjc2ZTMifQ.SlrY7a-r4hDvj0dmedZ5_2vAzOBcays_ETW6bPd6e6E");
        authenticationToken.setExpiresIn(7200);
        authenticationToken.setTokenType("bearer");
        return ResultInfo.ok(authenticationToken);
    }
    @DeleteMapping("/logout")
    public ResultInfo<?> logout() {
        return ResultInfo.ok();
    }
}